package com.mybatis.plugins;

import com.ibatis.common.logging.Log;
import com.ibatis.common.logging.LogFactory;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.internal.util.StringUtility;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Set;

/**
 * @author: xuKai
 * @date: 2024/3/6 12:31
 * @description:
 */
public class CommentPlugin implements CommentGenerator {
    Log                log = LogFactory.getLog(CommentGenerator.class);
    private Properties properties;
    private boolean    columnRemarks;

    public CommentPlugin() {
        super();
        this.properties = new Properties();
    }

    @Override
    public void addJavaFileComment(CompilationUnit compilationUnit) {
        compilationUnit.addFileCommentLine("/**");
        compilationUnit.addFileCommentLine(" * " + compilationUnit.getType().getShortName() + "类");
        compilationUnit.addFileCommentLine(" * " + compilationUnit.getType().getPackageName() + "");
        compilationUnit.addFileCommentLine(" */");
    }

    /**
     * Adds a suitable comment to warn users that the element was generated, and
     * when it was generated.
     */
    @Override
    public void addComment(XmlElement xmlElement) {
        xmlElement.addElement(new TextElement("<!-- 此文件由mybatis-generator插件自动生成。请不要对此文件做任何修改！ -->"));
    }

    @Override
    public void addRootComment(XmlElement rootElement) {
        TextElement e = new TextElement("<!-- 此文件由mybatis-generator插件自动生成。请不要对此文件做任何修改！\n       如果您需要自定义sql，建议另外添加一个extra扩展文件!!!\n"
                + "       ps:extra扩展文件中不需要重新定义基础的resultMap和sql代码，只需要在此文件中添加额外的sql片段。-->");
        rootElement.addElement(e);
    }

    @Override
    public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * " + method.getName() + " 方法");
        method.addJavaDocLine(" */");

    }

    @Override
    public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn,
                                           Set<FullyQualifiedJavaType> set) {
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * " + method.getName() + " 方法");
        method.addJavaDocLine(" */");

    }

    @Override
    public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {
        field.addJavaDocLine("/**");
        field.addJavaDocLine(" * " + field.getName() + " 字段");
        field.addJavaDocLine(" */");

    }

    @Override
    public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn,
                                   Set<FullyQualifiedJavaType> set) {
        field.addJavaDocLine("/**");
        field.addJavaDocLine(" * " + field.getName() + " 字段");
        field.addJavaDocLine(" */");

    }

    @Override
    public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set) {
        innerClass.addJavaDocLine("/**");
        innerClass.addJavaDocLine(" * " + innerClass.getType().getShortName() + " 类");
        innerClass.addJavaDocLine(" */");

    }

    @Override
    public void addConfigurationProperties(Properties properties) {
        this.properties.putAll(properties);
        this.columnRemarks = StringUtility.isTrue(properties.getProperty("columnRemarks"));
    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
        innerClass.addJavaDocLine("/**");
        innerClass.addJavaDocLine(" * " + innerClass.getType().getShortName() + " 类");
        innerClass.addJavaDocLine(" * " + introspectedTable.getFullyQualifiedTable().getIntrospectedTableName() + " 表");
        innerClass.addJavaDocLine(" * " + introspectedTable.getRemarks() + "");
        innerClass.addJavaDocLine(" */");
    }

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        topLevelClass.addJavaDocLine("/**");
        topLevelClass.addJavaDocLine(" * @date " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
        topLevelClass.addJavaDocLine(" * @author xuKai");
        topLevelClass.addJavaDocLine(" * @desc " + introspectedTable.getFullyQualifiedTable().getIntrospectedTableName() + " 表");
        topLevelClass.addJavaDocLine(" */");

    }

    @Override
    public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
            field.addJavaDocLine("/**");
            StringBuilder sb = new StringBuilder();
            sb.append(" * ");
            sb.append(introspectedColumn.getRemarks() == null ? "" : introspectedColumn.getRemarks());
            field.addJavaDocLine(sb.toString());
            field.addJavaDocLine(" */");

        }

    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
        field.addJavaDocLine("/**");
        field.addJavaDocLine(" * " + introspectedTable.getFullyQualifiedTable().getIntrospectedTableName() + " 表");
        field.addJavaDocLine(" */");
    }

    @Override
    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * " + method.getName() + " 方法");
        method.addJavaDocLine(" */");
    }

    @Override
    public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        method.addJavaDocLine("/**");
        StringBuilder sb = new StringBuilder();
        sb.append(" * 获取 ");
        sb.append(introspectedColumn.getRemarks());
        sb.append(" 字段值");
        method.addJavaDocLine(sb.toString());
        method.addJavaDocLine(" */");

    }

    @Override
    public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        method.addJavaDocLine("/**");
        StringBuilder sb = new StringBuilder();
        sb.append(" * 设置 ");
        sb.append(introspectedColumn.getRemarks());
        sb.append(" 字段值");
        method.addJavaDocLine(sb.toString());
        method.addJavaDocLine(" */");
    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
        innerClass.addJavaDocLine("/**"); //$NON-NLS-1$
        innerClass.addJavaDocLine(" */"); //$NON-NLS-1$
    }

}